﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;

namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class DatabaseCheckMdfFileonCdrive : IssueTypeCheckBase
    {
       public DatabaseCheckMdfFileonCdrive()
        {
            IssueTypeId = 3003;
        }

       public override void Check()
       {
           // Create a Data Context 
           HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);
           Issue issue ;

           // Create a link Query ( query Selects a database that has the MDF file stored on the C Drive 
           var qry = from d in dc.Databases
                     from df in d.DatabaseFiles
                     where ((df.File.Contains("mdf") && df.Drive.Contains("C")))
                     select d;

           foreach(var database in qry)
           {
               int databaseId = database.DatabaseID;
               //Get Issuetype object

               issue = new Issue();

               //Get Issuetype object  And Create a new  Global issue object 
               IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);
               
               issue.IssueType = issueType;
               issue.TargetId = database.DatabaseID;
               issue.Description = issueType.Description.Replace("{Database}", database.DatabaseName);
              
               dc.Issues.InsertOnSubmit(issue);
           }
          
           dc.SubmitChanges();    

       }
    
      
    
    }
}
